using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;

namespace Scene.Scripts
{
    [Serializable]
    public class SceneAssets : MonoBehaviour
    {
        [Serializable]
        public class SceneObjWrap
        {
            public SceneObj obj;
            public SceneBox.AssetDetailType detailType;
            public List<SceneBox> belongBoxes;
        }
        [Serializable]
        public struct SceneObjData
        {
            public SceneBox.AssetDetailType assetType;
            public List<int> sceneBoxIds;
            [SerializeField]
            public MeshMatData sceneObj;
            [SerializeField]
            public DynamicObjData dynamicObj;
        }

        // [SerializeField] private List<SceneObjWrap> mSceneDates;
        [SerializeField] private Bounds mSceneBounds;

        [SerializeField] public List<SceneObjData> sceneDatas;

#if UNITY_EDITOR

        [SerializeField]
        private string debugAssetName;
        [SerializeField]
        private bool debugAsset;
        public void EditorAdd(List<SceneObjWrap> list, Bounds bounds)
        {
            mSceneBounds = bounds;
            // mSceneDates = new List<SceneObjWrap>();
            // mSceneDates.AddRange(list.ToArray());
            sceneDatas = new List<SceneObjData>(1000);
            foreach (var item in list)
            {
                var objData = new SceneObjData();
                objData.assetType = item.detailType;
                objData.sceneBoxIds = new List<int>(item.belongBoxes.Count);
                if (item.detailType != SceneBox.AssetDetailType.DynamicType)
                {
                    objData.sceneObj = item.obj as MeshMatData;   
                }
                else
                {
                    objData.dynamicObj = item.obj as DynamicObjData;
                }
                foreach (var bb in item.belongBoxes)
                {
                    objData.sceneBoxIds.Add(bb.BoxId);
                }

                sceneDatas.Add(objData);
            }
        }

        private void OnDrawGizmos()
        {
            if (GlobalAssets.Inst != null && GlobalAssets.Inst.mShowSceneObjBounds)
            {
                if (sceneDatas != null)
                {
                    foreach (var data in sceneDatas)
                    {
                        Gizmos.DrawWireCube(data.sceneObj.objBounds.center, data.sceneObj.objBounds.size);
                    }

                    Gizmos.DrawWireCube(mSceneBounds.center, mSceneBounds.size);
                }
            }

            if (debugAsset)
            {
                foreach (var item in sceneDatas)
                {
                    /*if (item.sceneObj.objTransform.name == debugAssetName)
                    {
                        foreach (var boxId in item.sceneBoxIds)
                        {
                            var bounds = SceneBox.FindBoxBounds(boxId);
                            Gizmos.DrawCube(bounds.center, bounds.size);
                        }
                        //item.sceneBoxIds
                    }*/
                }
            }
        }
#endif
        private void Awake()
        {
            if (Application.isPlaying == false)
                return;

            SceneBox.OnSceneAssetLoaded(this);
        }
    }
}